"""定时任务   用于提取代理ip和检测ip可用性"""
from concurrent import futures
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
from proxyip.models import ProxyIp

scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")

# 提取ip的定时任务  40s提取一个
@register_job(scheduler, "interval", seconds=40, replace_existing=True)
def job_extract_ip():
    ProxyIp.extract_ip()
    # time.sleep(random.randrange(1,100,1)/100.)
    # print("I am a test Job!!")

# 检测ip可用性的定时任务    50秒检测一次
@register_job(scheduler, "interval", seconds=50, replace_existing=True)
def job_check_ip():
    ipobjs = ProxyIp.objects.filter(valid=True)
    num = ipobjs.count()
    if num > 15:
        num = 15
    with futures.ThreadPoolExecutor(num) as executor:
        executor.map(ProxyIp.check_ip_valid, ipobjs)


register_events(scheduler)

scheduler.start()
print("定时任务开启!!")
